package com.accounting.juanjuan.service;

import cn.hutool.poi.excel.ExcelReader;
import com.accounting.juanjuan.constant.EnforcementEnum;
import com.accounting.juanjuan.entity.AmountData;
import com.accounting.juanjuan.entity.ComputeAccountingData;
import com.accounting.juanjuan.entity.ResultData;
import com.accounting.juanjuan.exception.AppException;
import com.accounting.juanjuan.exception.BizException;
import com.accounting.juanjuan.utils.*;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * 服务类，不操作数据库，不带事务，以后涉及数据再改
 *
 * @Author xxj
 * @Date 2024/1/27 21:34
 * @Version 1.0
 */
@Service
public class AccountingService {
    /**
     * 根据excel文件获取标题,用于下拉框
     */
    public List<String> getExcelTitle(MultipartFile file, String sheetName) {
        try (InputStream inputStream = file.getInputStream()) {
            ExcelReader excelReader = MyExcelUtil.getExcelReader(inputStream, sheetName);
            return MyExcelUtil.getTitle(excelReader);
        } catch (IOException exception) {
            throw new AppException("读取文件发生异常！");
        }
    }

    /**
     * 获取计算后的数据
     *
     * @return
     */
    public ResultData getComputeAfterData(MultipartFile file, String sheetName, String columnName, String enforcement) {
        try (InputStream inputStream = file.getInputStream()) {
            ResultData resultData = new ResultData();
            ExcelReader excelReader = MyExcelUtil.getExcelReader(inputStream, sheetName);
            if (!MyExcelUtil.getTitle(excelReader).contains(columnName)) {
                throw new BizException("你所指定的列不存在！");
            }
            //获取金额数据进行处理
            List<List<AmountData>> positiveAndNegativeAmountDataList = MyExcelUtil.getPositiveAndNegativeAmountDataList(excelReader, columnName);
            ComputeAccountingData computeAccountingData;
            enforcement = enforcement.toLowerCase(Locale.CHINESE);
            EnforcementEnum.checkExist(enforcement);
            if (EnforcementEnum.SMALL_FORCE.getValue().equals(enforcement)) {
                //小力度
                computeAccountingData = ComputeAccountingUtil.smallForce(positiveAndNegativeAmountDataList.get(0), positiveAndNegativeAmountDataList.get(1));
            } else if (EnforcementEnum.MEDIUM_STRENGTH.getValue().equals(enforcement)) {
                //中力度
                computeAccountingData = ComputeAccountingUtil.mediumStrength(positiveAndNegativeAmountDataList.get(0), positiveAndNegativeAmountDataList.get(1));
            } else {
                //没有大力度
                throw new BizException("执行力度不存在，无法执行！");
            }
            resultData.setValueOnComputeValue(computeAccountingData);
            //获取带源文件行号的标题
            List<Object> titleAddRowName = MyExcelUtil.getTitleAddRowName(excelReader);
            List<AmountData> amountDataList = computeAccountingData.getAmountDataList();
            //根据序号获取全部数据
            List<List<Object>> resultExcelData = new ArrayList<>();
            //先加入标题，带“源文件行号”
            resultExcelData.add(titleAddRowName);
            for (AmountData amountData : amountDataList) {
                //加入处理后的数据对应的原始数据，带“源文件行号”
                resultExcelData.add(MyExcelUtil.getRowDataAddRow(excelReader, amountData.getXh()));
            }
            resultData.setResultExcelData(ListUtil.objectList2StringList(resultExcelData));
            return resultData;
        } catch (IOException exception) {
            throw new AppException("读取文件发生异常！");
        }
    }

    /**
     * 根据传来的数据，生成对应的excel文件，并下载
     */
    public void downExcelFileOnData(HttpServletResponse response, String fileName, String columnName, String data) {
        List<List<String>> lists = ListUtil.stringList(data);
        List<List<Object>> listList = ListUtil.stringList2MoreType(lists, columnName);
        ActionUtil.writeStreamToResponseByExcel(response, StringUtil.getFileNameWithoutExt(fileName), listList);
    }
}
